// 集成德育素养答题弹窗逻辑的组件（结合 constants.dart 数据结构）
import 'package:flutter/material.dart';

import 'constants.dart';

class EvaluationDialog extends StatefulWidget {
  final String title;

  const EvaluationDialog({super.key, required this.title});

  @override
  State<EvaluationDialog> createState() => _EvaluationDialogState();
}

class _EvaluationDialogState extends State<EvaluationDialog> {
  late List<Map<String, dynamic>> questions;
  late List<Map<String, String>> suggestions;

  @override
  void initState() {
    super.initState();
    _loadDataByTitle(widget.title);
  }

  void _loadDataByTitle(String title) {
    if (title == "和润德育") {
      questions = moralQuestions;
      suggestions = moralSuggestions;
    } else if (title == "语文素养") {
      questions = chineseQuestions;
      suggestions = chineseSuggestions;
    } else if (title == "数学素养") {
      questions = mathQuestions;
      suggestions = mathSuggestions;
    } else {
      questions = [];
      suggestions = [];
    }
  }

  Map<int, String?> selectedSingleMap = {};
  Map<int, List<String>> selectedMultiMap = {};

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      insetPadding: const EdgeInsets.symmetric(horizontal: 15, vertical: 30),
      contentPadding: const EdgeInsets.all(15),
      title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(widget.title),
          IconButton(
            icon: const Icon(Icons.close),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ],
      ),
      content: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (questions.isNotEmpty) ...[
              const Text(
                "一、素养单选题（每题选一项）",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
              ),
              const SizedBox(height: 10),
              ...List.generate(questions.length, (index) {
                final questionData = questions[index];
                final options = questionData['options'] as List;

                selectedSingleMap.putIfAbsent(index, () => null);

                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      "${index + 1}. ${questionData['question']}",
                      style: const TextStyle(fontWeight: FontWeight.w600),
                    ),
                    const SizedBox(height: 4),
                    ...options.map(
                      (opt) => RadioListTile<String>(
                        title: Text(
                          "${opt['letter']}：${opt['text']}",
                          style: const TextStyle(fontSize: 14),
                        ),
                        value: opt['letter'],
                        contentPadding: const EdgeInsets.symmetric(
                          horizontal: 0,
                        ),
                        groupValue: selectedSingleMap[index],
                        onChanged: (value) {
                          setState(() {
                            selectedSingleMap[index] = value;
                          });
                        },
                      ),
                    ),
                  ],
                );
              }),
              const SizedBox(height: 20),
            ],

            if (suggestions.isNotEmpty) ...[
              const Text(
                "二、具体表现（多选题）",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
              ),
              const SizedBox(height: 10),
              ...List.generate(suggestions.length, (index) {
                final suggestion = suggestions[index];
                final suggestionText = suggestion['text'] ?? "";

                final options = [suggestionText.trim()];
                selectedMultiMap.putIfAbsent(index, () => []);

                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      "${suggestion['title']}",
                      style: const TextStyle(fontWeight: FontWeight.w600),
                    ),
                    const SizedBox(height: 4),
                    ...options.map(
                      (desc) => CheckboxListTile(
                        title: Text(desc),
                        contentPadding: const EdgeInsets.symmetric(
                          horizontal: 0,
                        ),
                        value: selectedMultiMap[index]!.contains(desc),
                        onChanged: (bool? checked) {
                          setState(() {
                            if (checked == true) {
                              selectedMultiMap[index]!.add(desc);
                            } else {
                              selectedMultiMap[index]!.remove(desc);
                            }
                          });
                        },
                      ),
                    ),
                  ],
                );
              }),
            ],
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () {
            print('单选题答案：$selectedSingleMap');
            print('多选题答案：$selectedMultiMap');
            Navigator.of(context).pop();
          },
          child: const Text("提交"),
        ),
      ],
    );
  }
}
